package fi.helsinki.cs.KaiSei;

import java.util.ArrayList;

public class Reports {
	private String type;
	private Schedule schedule;
	private int day;
	private String[] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

	public Reports(String type, Schedule schedule) {
		this.schedule = schedule;
		this.type = type;
	}

	public Reports(String type, Schedule schedule, int day) {
		this.type = type;
		new Reports(type, schedule);
		this.day = day;
		this.schedule = schedule;
	}

	@Override
	public String toString() {
		if (type.contains("DAY")) 
			return dayReport();
		if (type.contains("WEEK")) 
			return weekReport();
		if (type.contains("FULL")) 
			return fullReport();
		else return null;
	}

	private String dayReport() {

		ArrayList<Event> events = schedule.getEventList(this.day);
		if (events == null) {
			return null;
		}
		StringBuilder res = new StringBuilder();
		res.append(week[this.day] + ":\n------\n");

		for (Event e : events){
			if(e.getTitle().equals("")){
				res.append("<no title for this event>");
			}
			else{
				res.append(e.getTitle());
			}

			//event must always have start time and end time
			int start = Integer.parseInt(e.getStartTime());
			int end = Integer.parseInt(e.getEndTime());
			res.append("\nat " + start + " to " + end);

			if (e.getLocation().equals("")){
				res.append("\n<no location specified>");
			}
			else {
				res.append("\nin " + e.getLocation());
			}
			res.append("\n\n");
		}
		return res.toString();
	}

	private String weekReport() {

		String[][] res = new String[7 + 1][7]; // +1 for header row

		res[0][0] = "\t";

		for (int i = 1, j = 0; j < Event.VALID_START_TIMES.length; i++, j++){
			res[0][i] = Event.VALID_START_TIMES[j] + "\t";
		}

		int i = 1;
		for (int j = 0; j < 7; j++) {
			res[i][0] = week[j] + "\t";
			i++;
		}

		i = 1;
		for (int j = 0; j < 7; j++) {
			ArrayList<Event> events = this.schedule.getEventList(j);

			if (events == null){
				return null;
			}
			else if (events.size() == 0){
				for (int x = 1; x < 7; x++) {
					res[i][x] = "\t";
				}
			}

			for (Event event : events){
				String entry = "\t"; // if event is null

				if (event.getLocation() != null) {
					entry = event.getLocation()+"\t";
				}

				if (Integer.parseInt(event.getStartTime()) == 8)     { res[i][1] = entry; }
				else if(Integer.parseInt(event.getStartTime())  == 10) { res[i][2] = entry; }
				else if(Integer.parseInt(event.getStartTime()) == 12) { res[i][3] = entry; }
				else if(Integer.parseInt(event.getStartTime()) == 14) { res[i][4] = entry; }
				else if(Integer.parseInt(event.getStartTime()) == 16) { res[i][5] = entry; }
				else if(Integer.parseInt(event.getStartTime()) == 18) { res[i][6] = entry; }

				// fill up with empties
				for (int x = 1; x < 7; x++) {
					if (res[i][x] == null){
						res[i][x] = "\t";
					}
				}

			}
			i++;
		}

		StringBuilder response = new StringBuilder();

		for (int j = 0; j < res.length; j++){
			for (int k = 0; k < res[0].length; k++){
				response.append(res[j][k]);
			}
			response.append("\n");
		}

		return response.toString();
	}

	private String fullReport(){
		StringBuilder res = new StringBuilder();

		for (int j = 0; j < 7; j++) {
			res.append(week[j] + ":\n");
			res.append("----\n");
			for (Event event : schedule.getEventList(j)){
				res.append(event.getTitle());
				res.append("\nat " + event.getStartTime() + "-" + event.getEndTime());
				res.append("\nin " + event.getLocation());
			}
			res.append("\n\n");
		}

		return res.toString();
	}

}     